package jumpingnotes.dao.hibernate;

import java.util.List;
import java.lang.String;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import jumpingnotes.dao.AudioDAO;
import jumpingnotes.dao.Sorter;
import jumpingnotes.dao.SorterHelper;
import jumpingnotes.model.Audio;

public class AudioHibernateDAO extends GenericHibernateDAO<Audio> implements AudioDAO
{
	public AudioHibernateDAO()
	{
		super(Audio.class);
	}

	public List<Audio> findByKeyword(final String keyword, final int firstResult, final int maxResults, final Sorter sorter)
	{
		return (List<Audio>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("from Audio a where a.book.author like :author or a.book.resourceName like :name")
					.setString("author", "%"+keyword+"%").setString("name", "%"+keyword+"%");
				//SorterHelper.addSorter(query, sorter);
				if (firstResult >= 0)
					query.setFirstResult(firstResult);
				if (maxResults > 0)
					query.setMaxResults(maxResults);								
				return query.list();
			}
		}, true);
	}
	
	public List<Audio> findAllReading(final int firstResult, final int maxResults, final Sorter sorter) {
		return (List<Audio>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("from Audio a where a.sync is not null order by a.resourceId desc");
				//SorterHelper.addSorter(query, sorter);
				if (firstResult >= 0)
					query.setFirstResult(firstResult);
				if (maxResults > 0)
					query.setMaxResults(maxResults);								
				return query.list();
			}
		}, true);		
	}
	
	public List<Audio> findReadingByKeyword(final String keyword, final int firstResult, final int maxResults, final Sorter sorter) {
		return (List<Audio>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("from Audio a where a.sync is not null and (a.book.author like :author or a.book.resourceName like :name) order by a.resourceId desc")
					.setString("author", "%"+keyword+"%").setString("name", "%"+keyword+"%");
				//SorterHelper.addSorter(query, sorter);
				if (firstResult >= 0)
					query.setFirstResult(firstResult);
				if (maxResults > 0)
					query.setMaxResults(maxResults);								
				return query.list();
			}
		}, true);		
	}
	
	public Long getReadingCount() {
		return (Long) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select count(*) from Audio a where a.sync is not null");							
				return query.uniqueResult();
			}
		}, true);			
	}
	
	public Long getReadingCountByKeyword(final String keyword) {
		return (Long) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select count(*) from Audio a where a.sync is not null and (a.book.author like :author or a.book.resourceName like :name)")
					.setString("author", "%"+keyword+"%").setString("name", "%"+keyword+"%");						
				return query.uniqueResult();
			}
		}, true);			
	}	
}
